/**
 * @name        _directive.ts
 * @author      shenyc
 * @date        2020-05-06 - ?
 * @copyright   Copy right (c) shenyc (shenyczz@163.com).
 *              All rights reserved.
 *
 ******************************************************************************/
// {{@@@}}
"use strict"
// ----------------------------------------------------------------------------
import type { Directive, DirectiveBinding } from "vue"
import { ElMessage } from "element-plus"
interface ElType extends HTMLElement {
    copyData: string | number
}
// ----------------------------------------------------------------------------
/**
 * v-copy
 * @description 复制某个值至剪贴板
 * @接受参数 String | Ref&lt;string&gt; | Reactive&lt;string&gt;
 */
export const _directive_copy: Directive = {
    mounted(el: ElType, binding: DirectiveBinding) {
        el.copyData = binding.value
        el.addEventListener("click", handleClick)
    },
    updated(el: ElType, binding: DirectiveBinding) {
        el.copyData = binding.value
    },
    beforeUnmount(el: ElType) {
        el.removeEventListener("click", handleClick)
    },
}

async function handleClick(this: any) {
    try {
        await navigator.clipboard.writeText(this.copyData)
        ElMessage({
            type: "success",
            message: "复制成功",
        })
    } catch (err) {
        console.error("复制操作不被支持或失败: ", err)
    }
}
export default _directive_copy
// ----------------------------------------------------------------------------
// {{@@@}}
